// pages/my-balance/my-balance.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataTab: [
      {title: '账户明细',value: 1},
      {title: '提现记录',value: 2},
    ],
    TabCur: 1,

    wallet: {},
    balanceList: [],
    withdrawal: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({userInfo: app.globalData.userInfo})
    this.getUserBalance(app.globalData.userInfo.openid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },





  tabSelect(param){
    var v = param.currentTarget.dataset.value
    this.setData({TabCur : v})
  },
  getUserBalance(openid){
    var that = this
    wx.request({
      url: app.globalData.apiPath + 'wechat/userBalance',
      method: 'POST',
      data: {openid: openid},
      success (res) {
        console.log(res)
        var balanceList = res.data.data.userBalanceDetail
        balanceList.forEach(item => {
          item.showMoney = (item.availableTxb/100).toFixed(2)
          if(item.detailType == 1){
            item.showMoney = ((item.befAvailableTxb - item.availableTxb)/100).toFixed(2)
          }else if(item.detailType == 2){
            item.showMoney = ((item.availableTxb-item.befAvailableTxb)/100).toFixed(2)
          }
        })
        var withdrawal = res.data.data.withdrawal
        withdrawal.forEach(item => {
          item.amount = (item.amount/100).toFixed(2)
        })
        var userWallet = res.data.data.userWallet
        userWallet.availableTxb = (userWallet.availableTxb/100).toFixed(2)
        that.setData({wallet: userWallet,balanceList: balanceList, withdrawal: withdrawal})
      }
    })
  },
  tixian(){
    if(this.data.wallet.availableTxb < 10){
      wx.showToast({title: '提现金额不能低于10元',icon: 'none',duration: 2000}); return false;
    }
    console.log(app.globalData.userInfo.isAuthentication)
    if(app.globalData.userInfo.isAuthentication == 0){
      wx.showModal({
        title: '未认证',
        content: '请先完善您的个人信息，以便快速到账',
        confirmText: '前往认证',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({ url: '/pages/my-info/my-info'})
          } else if (res.cancel) { }
        }
      })
      return false;
    }

    var that = this
    var amount = that.data.wallet.availableTxb;
    wx.showModal({
      title: '提示',
      content: '是否全部提现'+ amount+'元？',
      confirmText: '提现',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.apiPath + 'withdrawal/setWithdrawal',
            method: 'POST',
            data: {openid: that.data.userInfo.openid,amount: amount*100},
            success (res) {
              console.log(res)
              if(res.data.code != 0){
                  wx.showToast({title: "提现失败",icon: 'error',duration: 2000 }); return false;
              }
              wx.showToast({title: "提现成功",icon: 'none',duration: 2000 })
              setTimeout(function(){
                wx.switchTab({url: '/pages/my/index'})
              },2000)
            }
          })
        } else if (res.cancel) { }
      }
    })

  },



})